---
title: "CREDI_R_models_weights"
date: "2024-04-03"
output: pdf_document
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown
```{r}
library(tidyverse)
library(lme4)
library(lmerTest)
library(haven)

```

## Upload and merge data

```{r}
dat <- read_dta("/Users/globalties/Library/CloudStorage/Box-Box/ECDEC - Shared Files/04_Projects/Play to Learn/HPL 2-4 Impact Evaluation, home-based centers/6_RCT/Data/Quantitative/Baseline/Resources/data/Analysis datasets/imp_anaysis_data.dta")
credi_m <- read_dta("/Users/globalties/Library/CloudStorage/Box-Box/ECDEC - Shared Files/04_Projects/Play to Learn/HPL 2-4 Impact Evaluation, home-based centers/6_RCT/Data/Quantitative/Baseline/Resources/data/Analysis datasets/final/CREDI_Mother_data_all_ages.dta")
colnames(credi_m)[1] <- "hhid_final"

datm <- left_join(credi_m,dat, by="hhid_final")
```


## Overall sample - using weights

```{r}
## drop outliers
datm <- datm[!(datm$hhid_final %in% c("H-0279-C-70207", "H-0288-C-73355", "H-0455-C-77043",
                              "H-0428-C-70328","H-0189-C-76576","H-0402-C-71403",
                              "H-0179-C-75382","H-0376-C-77518","H-0179-C-75382",
                              "H-0177-C-69709")), ]


## WEIGHTS
datm <- datm %>% mutate(weight_COG=1/((COG_SE)^2),
                        weight_LANG=1/((LANG_SE)^2),
                        weight_MOT=1/((MOT_SE)^2),
                        weight_SEM=1/((SEM_SE)^2))


fitCOGw <- lmer(data=datm, COG ~ tx + camp+ `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+as.factor(fb_q4_edu) +`_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +`_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +
                  camp:`_1_f_age` +camp:`_1_m_age` +camp:fb_grade +camp:as.factor(fb_q4_edu) +camp:`_1_child_fem` +camp:AGE +camp:`_1_mb_q2_hunger` +camp:`_1_mb_q4_pregnant` +camp:`_1_mb_q2_fin` +camp:`_1_hh_res` + 
                  (1|mvid), REML=FALSE, weights = weight_COG)

summary(fitCOGw)            


fitLANGw <- lmer(data=datm, LANG ~ tx + camp+ `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                  as.factor(fb_q4_edu) +
                  `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                  `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +camp:`_1_f_age` +
                  camp:`_1_m_age` +camp:fb_grade +camp:as.factor(fb_q4_edu) +camp:`_1_child_fem` +camp:AGE +
                  camp:`_1_mb_q2_hunger` +camp:`_1_mb_q4_pregnant` +camp:`_1_mb_q2_fin` +camp:`_1_hh_res` +
                  (1|mvid/clu2), REML=FALSE, weights = weight_LANG)

summary(fitLANGw)               

fitMOTw <- lmer(data=datm, MOT ~ tx + camp+ `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                 as.factor(fb_q4_edu) +
                 `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                 `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +camp:`_1_f_age` +
                 camp:`_1_m_age` +camp:fb_grade +camp:as.factor(fb_q4_edu) +camp:`_1_child_fem` +camp:AGE +
                 camp:`_1_mb_q2_hunger` +camp:`_1_mb_q4_pregnant` +camp:`_1_mb_q2_fin` +camp:`_1_hh_res` +
                 (1|mvid/clu2), REML=FALSE, weights = weight_MOT)


fitSEMw <- lmer(data=datm, SEM ~ tx + camp+ 
                 `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+as.factor(fb_q4_edu) +
                 `_1_child_fem` +AGE+ 
                 as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` + `_1_mb_child_count`+ 
                 as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +
                 camp:`_1_f_age` +camp:`_1_m_age` +camp:fb_grade +camp:as.factor(fb_q4_edu) +
                 camp:`_1_child_fem` +camp:AGE +
                 camp:`_1_mb_q2_hunger` +camp:`_1_mb_q4_pregnant` +
                 camp:`_1_mb_q2_fin` +camp:`_1_hh_res` +
                 (1|mvid/clu2), REML=FALSE, weights = weight_SEM)
summary(fitSEMw)

```

## CAMP sample

```{r}

datc <- datm %>% filter(camp==1)
fitCOGcw <- lmer(data=datc, COG ~ tx + `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                  as.factor(fb_q4_edu) +
                  `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                  `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`+
                  (1|mvid), REML=FALSE, weights = weight_COG)

summary(fitCOGcw)            


fitLANGcw <- lmer(data=datc, LANG ~ tx + `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                   as.factor(fb_q4_edu) +
                   `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                   `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua` +
                   (1|mvid), REML=FALSE, weights = weight_LANG)

summary(fitLANGcw)               

fitMOTcw <- lmer(data=datc, MOT ~ tx + `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                  as.factor(fb_q4_edu) +
                  `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                  `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`+
                  (1|mvid/clu2), REML=FALSE, weights = weight_MOT)

summary(fitMOTcw)  

fitSELcw <- lmer(data=datc, SEM ~ tx + 
                  `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+as.factor(fb_q4_edu) +
                  `_1_child_fem` +AGE+ 
                  as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` + `_1_mb_child_count`+ 
                  as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +
                  (1|mvid), REML=FALSE, weights = weight_SEM)
summary(fitSELcw)

```



## HOST sample

```{r}
dath <- datm %>% filter(host==1)
fitCOGhw <- lmer(data=dath, COG ~ tx + `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                   as.factor(fb_q4_edu) +
                   `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                   `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`+
                   (1|mvid/clu2), REML=FALSE, weights = weight_COG)

summary(fitCOGhw)            


fitLANGhw <- lmer(data=dath, LANG ~ tx + `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                    as.factor(fb_q4_edu) +
                    `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                    `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`+
                    (1|mvid/clu2), REML=FALSE, weights = weight_LANG)

summary(fitLANGhw)               

fitMOThw <- lmer(data=dath, MOT ~ tx + `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+
                   as.factor(fb_q4_edu) +
                   `_1_child_fem` +AGE+ as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` +
                   `_1_mb_child_count`+ as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`+
                   (1|mvid/clu2), REML=FALSE, weights = weight_MOT)

summary(fitMOThw)  

fitSELhw <- lmer(data=dath, SEM ~ tx + 
                   `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+as.factor(fb_q4_edu) +
                   `_1_child_fem` +AGE+ 
                   as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` + `_1_mb_child_count`+ 
                   as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +
                   (1|mvid/clu2), REML=FALSE, weights = weight_SEM)
summary(fitSELhw)
```

# Overall TABLE
```{r}
#overall cog
overall_cog_w_est=coef(summary(as(fitCOGw,"lmerModLmerTest")))["tx","Estimate"]
overall_cog_w_p  =coef(summary(as(fitCOGw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

#overall lang
overall_lang_w_est=coef(summary(as(fitLANGw,"lmerModLmerTest")))["tx","Estimate"]
overall_lang_w_p  =coef(summary(as(fitLANGw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

#overall mot
overall_mot_w_est=coef(summary(as(fitMOTw,"lmerModLmerTest")))["tx","Estimate"]
overall_mot_w_p  =coef(summary(as(fitMOTw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

#overall sel
overall_sel_w_est=coef(summary(as(fitSEMw,"lmerModLmerTest")))["tx","Estimate"]
overall_sel_w_p  =coef(summary(as(fitSEMw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

overll_w_coeffs=c(overall_cog_w_est,overall_lang_w_est,overall_mot_w_est,overall_sel_w_est)
overll_w_ps=c(overall_cog_w_p,overall_lang_w_p,overall_mot_w_p,overall_sel_w_p)
overall_table=cbind(overll_w_coeffs,overll_w_ps)
rownames(overall_table) <- c("Cognitive", "Language", "Motor", "SEL")
colnames(overall_table) <- c("Coefficient", "p-value")
overall_table <- round(overall_table, 3)
knitr::kable(overall_table)
```

# CAMP TABLE
```{r}
#overall cog
cogc_w_est=coef(summary(as(fitCOGcw,"lmerModLmerTest")))["tx","Estimate"]
cogc_w_p  =coef(summary(as(fitCOGcw,"lmerModLmerTest")))["tx","Pr(>|t|)"]
#overall lang
langc_w_est=coef(summary(as(fitLANGcw,"lmerModLmerTest")))["tx","Estimate"]
langc_w_p  =coef(summary(as(fitLANGcw,"lmerModLmerTest")))["tx","Pr(>|t|)"]
#overall mot
motc_w_est=coef(summary(as(fitMOTcw,"lmerModLmerTest")))["tx","Estimate"]
motc_w_p  =coef(summary(as(fitMOTcw,"lmerModLmerTest")))["tx","Pr(>|t|)"]
#overall mot
selc_w_est=coef(summary(as(fitSELcw,"lmerModLmerTest")))["tx","Estimate"]
selc_w_p  =coef(summary(as(fitSELcw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

cw_coeffs=c(cogc_w_est,langc_w_est,motc_w_est,selc_w_est)
cw_ps=c(cogc_w_p,langc_w_p,motc_w_p,selc_w_p)
table_c=cbind(cw_coeffs,cw_ps)
rownames(table_c) <- c("Cognitive", "Language", "Motor", "SEL")
colnames(table_c) <- c("Coefficient", "p-value")
table_c <- round(table_c, 3)
knitr::kable(table_c)
```



# HOST TABLE
```{r}
#overall cog
cogh_w_est=coef(summary(as(fitCOGhw,"lmerModLmerTest")))["tx","Estimate"]
cogh_w_p  =coef(summary(as(fitCOGhw,"lmerModLmerTest")))["tx","Pr(>|t|)"]
#overall lang
langh_w_est=coef(summary(as(fitLANGhw,"lmerModLmerTest")))["tx","Estimate"]
langh_w_p  =coef(summary(as(fitLANGhw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

#overall mot
moth_w_est=coef(summary(as(fitMOThw,"lmerModLmerTest")))["tx","Estimate"]
moth_w_p  =coef(summary(as(fitMOThw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

#overall mot
selh_w_est=coef(summary(as(fitSELhw,"lmerModLmerTest")))["tx","Estimate"]
selh_w_p  =coef(summary(as(fitSELhw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

hw_coeffs=c(cogh_w_est,langh_w_est,moth_w_est,selh_w_est)
hw_ps=c(cogh_w_p,langh_w_p,moth_w_p,selh_w_p)
table_h=cbind(hw_coeffs,hw_ps)
rownames(table_h) <- c("Cognitive", "Language", "Motor", "SEL")
colnames(table_h) <- c("Coefficient", "p-value")
table_h <- round(table_h, 3)
knitr::kable(table_h)
```